package 力扣题库;

public class p125验证回文串 {

    public  boolean isPalindrome(String s) {
        //在原字符串上进行双指针
        int left=0;
        int right=s.length()-1;

        while (left<=right) {
            while (left<right && !Character.isLetterOrDigit(s.charAt(left))) left++;
            while (left<right && !Character.isLetterOrDigit(s.charAt(right))) right--;

            if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
                return false;
            }
            left++;
            right--;
        }
        return true;

    }

    public  boolean isPalindrome2(String s) {
        //双指针
        StringBuilder str=new StringBuilder();
        for (int i = 0; i <s.length(); i++) {
            char c=s.charAt(i);
            if (Character.isLetterOrDigit(c)) {
                str.append(Character.toLowerCase(c));
            }
        }

        int left=0;
        int right=str.length()-1;
        while (left<=right) {
            if (str.charAt(left)!=str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }



    public boolean isPalindrome1(String s) {
        //取出每个字符，对其进行反转，判断前后是否相同

        int len=s.length();
        if (len==0) {
            return true;
        }

        StringBuilder str=new StringBuilder();
        for (int i = 0; i <len; i++) {
            char c=s.charAt(i);
            if (Character.isLetterOrDigit(c)) {
                //Character.isLetterOrDigit(c)  判断字符c 是否为 字母或数字
                str.append(Character.toLowerCase(c));
            }
        }

        StringBuilder strReverse=new StringBuilder(str).reverse();
        return str.toString().equals(strReverse.toString());
    }



}
